package com.jack.online.study.component;

import com.jack.online.study.mapper.edu.CourseDescriptionMapper;
import com.jack.online.study.mapper.edu.CourseMapper;
import com.jack.online.study.mapper.edu.SubjectMapper;
import com.jack.online.study.mapper.oss.FileMapper;
import com.jack.online.study.model.edu.dao.Course;
import com.jack.online.study.model.edu.dao.CourseDescription;
import com.jack.online.study.model.edu.dao.Subject;
import com.jack.online.study.model.edu.form.CourseInfoForm;
import com.jack.online.study.model.oss.dao.ImageDao;
import com.jack.online.study.service.edu.SubjectService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;


import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import static com.jack.online.study.common.constant.CourseDescriptions.*;

@Component
@Slf4j
public class DataMaker implements ApplicationRunner {


    @Resource
    private ResourceLoader resourceLoader;

    @Resource
    private SubjectService subjectService;
    @Resource
    private FileMapper fileMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private CourseDescriptionMapper courseDescriptionMapper;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        initSubjectSort();
        initTeacher();
        initAd();
        initCourse();
    }

    public void initCourse(){
        try {
            ClassPathResource java = new ClassPathResource("others/photo/course/java.jpg");
            ClassPathResource python = new ClassPathResource("others/photo/course/python.jpg");
            ClassPathResource mysql = new ClassPathResource("others/photo/course/mysql.jpg");
            ClassPathResource html = new ClassPathResource("others/photo/course/html.jpg");
            ClassPathResource linux = new ClassPathResource("others/photo/course/linux.jpg");
            ClassPathResource springmvc = new ClassPathResource("others/photo/course/springmvc.jpg");
            ClassPathResource java11 = new ClassPathResource("others/photo/course/java11.jpg");
            ClassPathResource dianshang = new ClassPathResource("others/photo/course/dianshang.jpg");
            ClassPathResource course9 = new ClassPathResource("others/photo/course/01.jpg");
            String javaUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 200, java.getFilename());
            String pythonUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 201, python.getFilename());
            String mysqlUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 202, mysql.getFilename());
            String htmlUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 203, html.getFilename());
            String linuxUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 204, linux.getFilename());
            String springmvcUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 205, springmvc.getFilename());
            String java11Url = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 206, java11.getFilename());
            String dianshangUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", 207, dianshang.getFilename());
            fileMapper.insertImage(new ImageDao(200L, "course", java.getFilename(), javaUrl, java.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(201L, "course", python.getFilename(), pythonUrl, python.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(202L, "course", mysql.getFilename(), mysqlUrl, mysql.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(203L, "course", html.getFilename(), htmlUrl, html.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(204L, "course", linux.getFilename(), linuxUrl, linux.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(205L, "course", springmvc.getFilename(), springmvcUrl, springmvc.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(206L, "course", java11.getFilename(), java11Url, java11.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(207L, "course", dianshang.getFilename(), dianshangUrl, dianshang.getContentAsByteArray()));


            CourseInfoForm javaCourse = new CourseInfoForm(null,"1","9","1","30天搞定java核心技术",new BigDecimal("0"),30,javaUrl,JavaCourseDesc);
            CourseInfoForm pythonCourse = new CourseInfoForm(null,"1","10","1","Python包教包会",new BigDecimal("0"),20,pythonUrl,PythonCourseDesc);
            CourseInfoForm mysqlCourse = new CourseInfoForm(null,"4","17","5","mysql精讲",new BigDecimal("0"),22,mysqlUrl,MysqlCourseDesc);
            CourseInfoForm htmlCourse = new CourseInfoForm(null,"2","11","2","手把手教你学会html",new BigDecimal("0"),18,htmlUrl,HtmlCourseDesc);
            CourseInfoForm linuxCourse = new CourseInfoForm(null,"5","15","4","Linux集群教程,打开千万并发大门的钥匙",new BigDecimal("0"),5,linuxUrl,LinuxCourseDesc);
            CourseInfoForm springmvcCourse = new CourseInfoForm(null,"6","9","1","springmvc从入门到放弃",new BigDecimal("0"),5,springmvcUrl,SpringmvcCourseDesc);
            CourseInfoForm java11Course = new CourseInfoForm(null,"2","9","1","java11新特性抢先知",new BigDecimal("0"),10,java11Url,Java11CourseDesc);
            CourseInfoForm dianshangCourse = new CourseInfoForm(null,"2","9","1","电商项目实战",new BigDecimal("0"),10,dianshangUrl,DianShangCourseDesc);


            addOneCourse(javaCourse);
            addOneCourse(pythonCourse);
            addOneCourse(mysqlCourse);
            addOneCourse(htmlCourse);
            addOneCourse(linuxCourse);
            addOneCourse(springmvcCourse);
            addOneCourse(java11Course);
            addOneCourse(dianshangCourse);

        } catch (Exception e) {
            log.error("初始化课程出错", e);
        }

    }
    public void addOneCourse(CourseInfoForm courseInfoForm){
        try {
            //保存课程基本信息
            Course course = new Course();
            course.setStatus(Course.COURSE_NORMAL);
            BeanUtils.copyProperties(courseInfoForm, course);
            courseMapper.insert(course);
            //保存课程详情信息
            CourseDescription courseDescription = new CourseDescription();
            courseDescription.setDescription(courseInfoForm.getDescription());
            courseDescription.setId(course.getId());
            courseDescriptionMapper.insert(courseDescription);
        } catch (Exception e) {
            log.error("添加一门课程失败",e);
        }
    }

    /**
     * 初始化广告数据
     */
    public void initAd() {
        try {
            ClassPathResource banner1 = new ClassPathResource("others/photo/banner/01.jpg");
            ClassPathResource banner2 = new ClassPathResource("others/photo/banner/02.jpg");
            ClassPathResource banner3 = new ClassPathResource("others/photo/banner/03.jpg");
//            ClassPathResource banner4 = new ClassPathResource("others/photo/banner/1442295581911.jpg");
            String imageUrl1 = String.format("""
                    http://localhost:9001/admin/oss/file/getImage/%s/%s""", 100, banner1.getFilename());
            String imageUrl2 = String.format("""
                    http://localhost:9001/admin/oss/file/getImage/%s/%s""", 101, banner2.getFilename());
            String imageUrl3 = String.format("""
                    http://localhost:9001/admin/oss/file/getImage/%s/%s""", 102, banner3.getFilename());
//            String imageUrl4 = String.format("""
//                    http://localhost:9001/admin/oss/file/getImage/%s/%s""", 103, banner4.getFilename());
            fileMapper.insertImage(new ImageDao(100L, "ad", banner1.getFilename(), imageUrl1, banner1.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(101L, "ad", banner2.getFilename(), imageUrl2, banner2.getContentAsByteArray()));
            fileMapper.insertImage(new ImageDao(102L, "ad", banner3.getFilename(), imageUrl3, banner3.getContentAsByteArray()));
//            fileMapper.insertImage(new ImageDao(103L, "ad", banner4.getFilename(), imageUrl4, banner4.getContentAsByteArray()));
        } catch (Exception e) {
            log.error("初始广告出错", e);
        }
    }

    /**
     * 初始化导入学科分类excel
     */
    public void initSubjectSort() {
        try {
            //一级学科
            Subject subject1 = new Subject("1","后端开发","0",0,null,null);
            Subject subject2 = new Subject("2","前端开发","0",0,null,null);
            Subject subject3 = new Subject("3","云计算","0",0,null,null);
            Subject subject4 = new Subject("4","系统/运维","0",0,null,null);
            Subject subject5 = new Subject("5","数据库","0",0,null,null);
            Subject subject6 = new Subject("6","大数据","0",0,null,null);
            Subject subject7 = new Subject("7","人工智能","0",0,null,null);
            Subject subject8 = new Subject("8","编程语言","0",0,null,null);
            //二级学科
            Subject subject9 = new Subject("9","Java","1",0,null,null);
            Subject subject10 = new Subject("10","Python","1",0,null,null);
            Subject subject11 = new Subject("11","HTML/CSS","2",0,null,null);
            Subject subject12 = new Subject("12","JavaScript","2",0,null,null);
            Subject subject13 = new Subject("13","Docker","3",0,null,null);
            Subject subject14 = new Subject("14","Linux","3",0,null,null);
            Subject subject15 = new Subject("15","Linux","4",0,null,null);
            Subject subject16 = new Subject("16","Windows","4",0,null,null);
            Subject subject17 = new Subject("17","MySQL","5",0,null,null);
            Subject subject18 = new Subject("18","MongoDB","5",0,null,null);
            Subject subject19 = new Subject("19","Hadoop","6",0,null,null);
            Subject subject20 = new Subject("20","Spark","6",0,null,null);
            Subject subject21 = new Subject("21","Python","7",0,null,null);
            Subject subject22 = new Subject("22","Java","8",0,null,null);
            subjectMapper.insert(subject1);
            subjectMapper.insert(subject2);
            subjectMapper.insert(subject3);
            subjectMapper.insert(subject4);
            subjectMapper.insert(subject5);
            subjectMapper.insert(subject6);
            subjectMapper.insert(subject7);
            subjectMapper.insert(subject8);
            subjectMapper.insert(subject9);
            subjectMapper.insert(subject10);
            subjectMapper.insert(subject11);
            subjectMapper.insert(subject12);
            subjectMapper.insert(subject13);
            subjectMapper.insert(subject14);
            subjectMapper.insert(subject15);
            subjectMapper.insert(subject16);
            subjectMapper.insert(subject17);
            subjectMapper.insert(subject18);
            subjectMapper.insert(subject19);
            subjectMapper.insert(subject20);
            subjectMapper.insert(subject21);
            subjectMapper.insert(subject22);
//            ClassPathResource res = new ClassPathResource("others/excel/subjectSort.xls");
//            subjectService.batchImport(res.getInputStream());
        } catch (Exception e) {
            log.error("初始化导入学科出错", e);
        }
    }

    /**
     * 初始化讲师头像
     */
    public void initTeacher() {
        try {
            ResourcePatternResolver resolver = (ResourcePatternResolver) resourceLoader;
            String pattern = "classpath:others/photo/teacher/*";
            org.springframework.core.io.Resource[] resources = resolver.getResources(pattern);

            List<org.springframework.core.io.Resource> resourceList = Arrays.stream(resources)
                    .filter(resource -> resource.exists() && resource.isFile() && resource.isReadable()).toList();
            for (int i = 0; i < resourceList.size(); i++) {
                org.springframework.core.io.Resource resource = resourceList.get(i);
                byte[] data = resource.getContentAsByteArray();
                String imageUrl = String.format("""
                        http://localhost:9001/admin/oss/file/getImage/%s/%s""", i, resource.getFilename());
                fileMapper.insertImage(new ImageDao((long) i, i + "", resource.getFilename(), imageUrl, data));
            }
        } catch (Exception e) {
            log.error("初始化讲师头像出错", e);
        }
        log.info("初始化讲师头像成功");
    }
}
